package com.maxatime.controller;

import com.alibaba.fastjson.JSONObject;
import com.maxatime.common.constant.BusinessContents;
import com.maxatime.pojo.entity.Login;
import com.maxatime.service.LoginService;

import com.maxatime.util.MyContent;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName loginController
 * @Description TODO
 * @Author saxas
 * @Date 2024/8/13 11:22
 **/
@RestController
@RequestMapping("/login")
public class loginController {

    @Autowired
    LoginService loginService;

    @RequestMapping("/login")
    public Map<String, Object> login(@RequestBody String JsonStr, HttpSession session){
        JSONObject json = JSONObject.parseObject(JsonStr);

        String username = json.getString("username");

        String password = json.getString("password");
        Login login = new Login();
        login.setUsername(username);
        login.setPassword(password);
        Login lgo = loginService.log(username,password);

        Map<String, Object> m = new HashMap<>();
        if (lgo != null){
            //数据保存到SESSION
//            session.setAttribute(BusinessContents.PK_ID,lgo.getPkId());
            m.put(MyContent.CODE, "0000");
            m.put(MyContent.MSG, "登录成功");
        }
        else if(lgo==null){
        m.put(MyContent.CODE, "2222");
        m.put(MyContent.MSG, "账号或密码错误，请重新输入");
        }
        return m;
    }
}
